<?php

namespace backend\controllers;


use Yii;
use common\models\Comments;
use backend\models\CommentsSearch;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * CommentsController implements the CRUD actions for Comments model.
 */
class CommentsController extends MyController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Comments models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model = new Comments();

        return $this->render('index', [
            'model' => $model,
        ]);
    }
    /*
     * Update Redis
     * */
    public function actionUpdateRedis(){
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $model = new CommentsSearch();
        $comment_id_s = intval(Yii::$app->request->post('comment_id_s'));
        $comment_id_e = intval(Yii::$app->request->post('comment_id_e'));

        if(!empty($comment_id_s)){

            $updateModel = new CommentsSearch();

            if(isset($comment_id_e) && $comment_id_e > $comment_id_s){

                for( ; $comment_id_s <= $comment_id_e; $comment_id_s++){
                    $updateModel->updateRedis($comment_id_s);
                }
                //var_dump(1);die;
            }
            else {
                $updateModel->updateRedis($comment_id_s);
                //var_dump(2);die;
            }
            $result['status'] = 1;
            $result['msg'] = '刷新成功';
        }
        else{
            $result = array(
                'status' => 0,
                'msg'   => '参数错误'
            );
        }

        return $result;
    }

    /**
     * Displays a single Comments model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Comments model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Comments();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->comment_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Comments model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->comment_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Comments model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }


    public function actionTest(){

        $model = new CommentsSearch();
        $model->updateFeedbackRate(10002);
        $ret = Yii::$app->redis_comments->hgetall('feedback_Rate_10002');
        var_dump($ret);
    }

    /**
     * Finds the Comments model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Comments the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Comments::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
